package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public Member add(Member member) {
        memberDao.add(member);
        return member;//返回了web才能拿到用户id
    }

    @Override
    public List<Map<String, Object>> getMemberReportByAgeAndSex() {
        Integer maleNum=memberDao.queryMemberNumBySex(1);
        Integer femaleNum=memberDao.queryMemberNumBySex(2);
        Map<String,Object> map1=new HashMap<>();
        Map<String,Object> map2=new HashMap<>();
        map1.put("male", maleNum);
        map1.put("name", "male");
        map2.put("female", femaleNum);
        map2.put("name", "female");
        List<Member> list=memberDao.queryMemberIdCard();
        for (Member member : list) {
            String idCard = member.getIdCard();
            String substring = idCard.substring(6, 14);
            memberDao.updateBirthday(member.getId(),substring);
        }
        List<Map<String,Object>> list1=memberDao.findMemberNumByAge();
        list1.add(map1);
        list1.add(map2);
        return list1;
    }

    @Override
    public List<Member> getFamily(Integer id) {

        List<Member> members = memberDao.findMemberByParentId(id);
        Member member = memberDao.findMemberById(id);
        members.add(member);
        return members;


    }
}
